package com.orangelife.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

public class PropertyUtils {

	/**
	 * 属性转为数据库字段
	 * 
	 * @param property
	 * @return
	 */
	public static String propertyToField(String pro) {
		if (null == pro) {
			return "";
		}
		String property = pro.replaceAll("ID", "Id");
		for (int i = 0; i < 26; i++) {
			String temp = property.replaceAll((char) ('A' + i) + "Id",
					(char) ('A' + i) + "id");
			property = temp;
		}
		char[] chars = property.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (char c : chars) {
			if (CharUtils.isAsciiAlphaUpper(c)) {
				sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 数据库转为字段属性
	 * 
	 * @param field
	 * @return
	 */
	public static String fieldToProperty(String fi) {
		if (null == fi) {
			return "";
		}

		String field = fi.replaceAll("_id", "_ID");
		for (int i = 0; i < 26; i++) {
			String temp = field.replaceAll("_" + (char) ('a' + i) + "id", "_"
					+ (char) ('a' + i) + "ID");
			field = temp;
		}

		char[] chars = field.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			if (c == '_') {
				int j = i + 1;
				if (j < chars.length) {
					sb.append(StringUtils.upperCase(CharUtils
							.toString(chars[j])));
					i++;
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	/**
	 * 将list内的数据库字段转为属性
	 * @param list
	 * @return
	 */
	public static List<Map<String, Object>> turnToEntity(
			List<Map<String, Object>> list) {

		List resultList = new ArrayList<>();

		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				Map map = new HashMap<>();
				Map m = (Map) list.get(i);
				/*
				 * Set keySet = m.keySet();// 返回键的集合 Iterator it =
				 * keySet.iterator(); while (it.hasNext()) // 第一种迭代方式取键值 {
				 * Object key = it.next();
				 * map.put(fieldToProperty(key.toString()), m.get(key));
				 * resultList.add(map);
				 * 
				 * }
				 */

				Iterator<Map.Entry<String, String>> it = m.entrySet()
						.iterator();
				while (it.hasNext()) {
					Map.Entry<String, String> entry = it.next();
					map.put(fieldToProperty(entry.getKey()), entry.getValue());

				}
				resultList.add(map);

			}
		}
		return resultList;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	/**
	 * 将entity内的数据库字段转为属性
	 * @param list
	 * @return
	 */
	public static Map<String, Object> turnToEntity1(Map<String, Object> entity) {
		if (entity == null) {
			return new HashMap<String, Object>();
		}

		Map map = new HashMap<>();

		if (entity != null) {
			Map m = (Map) entity;
			/*
			 * Set keySet = m.keySet();// 返回键的集合 Iterator it =
			 * keySet.iterator(); while (it.hasNext()) // 第一种迭代方式取键值 { Object
			 * key = it.next(); map.put(fieldToProperty(key.toString()),
			 * m.get(key)); resultList.add(map);
			 * 
			 * }
			 */

			Iterator<Map.Entry<String, String>> it = m.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, String> entry = it.next();
				map.put(fieldToProperty(entry.getKey()), entry.getValue());

			}
		}

		return map;
	}
}
